package org.example.user.service.impl;


import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.query.QueryWrapper;
import org.example.common.config.BusinessException;
import org.example.user.dto.RealnameAuthDTO;
import org.example.user.vo.AuthListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.example.user.service.ISysUserRealnameAuthService;
import org.example.user.entity.SysUserRealnameAuthEntity;
import org.example.user.mapper.SysUserRealnameAuthMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户实名认证信息表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Service
public class SysUserRealnameAuthServiceImpl extends ServiceImpl<SysUserRealnameAuthMapper, SysUserRealnameAuthEntity> implements ISysUserRealnameAuthService {


    @Autowired
    private SysUserRealnameAuthMapper sysUserRealnameAuthMapper;


    //实名认证
    @Override
    public int authSubmit(RealnameAuthDTO authDTO){
        //校验用户是否已认证
        SysUserRealnameAuthEntity sysUserRealnameAuthEntity = this.getUserAuthStatus();
        if(sysUserRealnameAuthEntity != null && sysUserRealnameAuthEntity.getAuthStatus() == 1 && sysUserRealnameAuthEntity.getAuthStatus() == 2){
            throw new BusinessException("已认证,不可修改");
        }

        SysUserRealnameAuthEntity newSysUserRealnameAuthEntity = new SysUserRealnameAuthEntity();

        newSysUserRealnameAuthEntity.setId(sysUserRealnameAuthEntity.getId());
        newSysUserRealnameAuthEntity.setUserId(StpUtil.getLoginIdAsLong());
        newSysUserRealnameAuthEntity.setRealName(authDTO.getRealName());
        newSysUserRealnameAuthEntity.setIdCard(authDTO.getIdCard());
        newSysUserRealnameAuthEntity.setIdCardFront("ff");
        newSysUserRealnameAuthEntity.setIdCardBack("ff");
        newSysUserRealnameAuthEntity.setAuthStatus(1);
        newSysUserRealnameAuthEntity.setCreatedAt(new Date());
        newSysUserRealnameAuthEntity.setUpdatedAt(new Date());

        return sysUserRealnameAuthMapper.update(newSysUserRealnameAuthEntity);
    }

    //获取用户认证状态
    @Override
    public SysUserRealnameAuthEntity getUserAuthStatus(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("*")
                .from(SysUserRealnameAuthEntity.class)
                .where(SysUserRealnameAuthEntity::getUserId).eq(StpUtil.getLoginIdAsLong());

        SysUserRealnameAuthEntity sysUserRealnameAuthEntity = sysUserRealnameAuthMapper.selectOneByQuery(queryWrapper);
        if(sysUserRealnameAuthEntity == null){
            return null;
        }
        return sysUserRealnameAuthEntity;
    }

    //管理员获取待审核实名认证列表
    @Override
    public List<AuthListVO> getAuthPendingList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("*")
                .from(SysUserRealnameAuthEntity.class)
                .where(SysUserRealnameAuthEntity::getAuthStatus).eq(1);
        List<SysUserRealnameAuthEntity> sysUserRealnameAuthEntities = sysUserRealnameAuthMapper.selectListByQuery(queryWrapper);
        List<AuthListVO> authListVO = sysUserRealnameAuthEntities.stream().map(sysUserRealnameAuthEntity -> {
            AuthListVO authItem = new AuthListVO();
            BeanUtils.copyProperties(sysUserRealnameAuthEntity, authItem);
            authItem.setSubmitTime(sysUserRealnameAuthEntity.getCreatedAt());
            return authItem;
        }).collect(Collectors.toList());
        return authListVO;
    }

    //管理员审核实名认证
    @Override
    public int authVerify(Long userId, Integer status, String rejectReason) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("*")
                .from(SysUserRealnameAuthEntity.class)
                .where(SysUserRealnameAuthEntity::getUserId).eq(userId);
        SysUserRealnameAuthEntity sysUserRealnameAuthEntity = sysUserRealnameAuthMapper.selectOneByQuery(queryWrapper);
        if(sysUserRealnameAuthEntity == null){
            throw new BusinessException("用户不存在");
        }
        if(status == 3){
            if(rejectReason == null || rejectReason.isEmpty()){
                throw new BusinessException("拒绝理由不能为空");
            }
        }else{
            sysUserRealnameAuthEntity.setRejectReason(null);
        }
        sysUserRealnameAuthEntity.setAuthStatus(status);
        sysUserRealnameAuthEntity.setRejectReason(rejectReason);
        sysUserRealnameAuthEntity.setUpdatedAt(new Date());
        return sysUserRealnameAuthMapper.update(sysUserRealnameAuthEntity);
    }
}